import express from "express";
import dotenv from "dotenv";
// import bodyParser from "body-parser";
import cookieJParser from "cookie-parser";
import cookieParser from "cookie-parser";
import path from "path";

import connectToMongoDB from "./db/connectToMongodb.js";
import authRoutes from "./routes/auth.routes.js";
import messageRoutes from "./routes/message.routes.js";
import userRoutes from "./routes/user.routes.js";
import { server, app } from "./ws/socket.js";

const __dirname = path.resolve();

// const app = express();
app.disable("x-powered-by"); // 取消响应头的x-powered-by信息 x-powered-by: Express
dotenv.config();
const PORT = process.env.PORT || 5000;
// app.use(bodyParser.json()); // request.body
app.use(express.json()); // request.body
app.use(cookieParser()); // request.cookies

// app.get("/", (req, res) => {
//   res.send("Hello World");
// });

app.use("/api/auth", authRoutes);
app.use("/api/messages", messageRoutes);
app.use("/api/users", userRoutes);

app.use(express.static(path.join(__dirname, "/frontend/dist")));
app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname, "frontend", "dist", "index.html"));
});

server.listen(PORT, () => {
  connectToMongoDB();
  console.log(`Server Running on port ${PORT}`);
});
